package com.hq.ims.data.event.listener;

import com.hq.ims.data.entity.Company;
import com.hq.ims.data.entity.CompanyEmployee;
import com.hq.ims.data.entityenum.EnumPlatformTodoType;
import com.hq.ims.data.entityenum.EnumUserTodoType;
import com.hq.ims.data.event.CompanyEvent;
import com.hq.ims.data.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

/**
 * 用户跟踪反馈事件
 *
 * @date 2022/6/7 15:41
 */
@Component
@Slf4j
public class CompanyListener {

    @Resource
    private BaseCompanyService baseCompanyService;
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private BasePlatformTodoStatisticsService basePlatformTodoStatisticsService;
    @Resource
    private BaseUserTodoStatisticsService baseUserTodoStatisticsService;
    @Resource
    private BaseBusiInfoService baseBusiInfoService;
    @Resource
    private BaseCompanyEmployeeService baseCompanyEmployeeService;

    @Async
    @TransactionalEventListener
    public void onApplicationEvent(CompanyEvent companyEvent) {
        String eventType = companyEvent.getEventType();

        switch (eventType) {
            case CompanyEvent.USER_JOIN_COMPANY:
                this.userJoinCompanyHandler(companyEvent);
                break;
            case CompanyEvent.REGISTER_COMPANY:
                this.registerCompanyHandler(companyEvent);
                break;
//            case CompanyEvent.EDIT_DISTRIBUTION_RULE:
//                this.editDistributionRuleHandler(companyEvent);
//                break;
            case CompanyEvent.PLATFORM_AUDIT_PASS_EVENT:
                this.platformAuditPassEventHandler(companyEvent);
                break;
            case CompanyEvent.PLATFORM_AUDIT_NOT_PASS_EVENT:
                this.platformAuditNotPassEventHandler(companyEvent);
                break;
            case CompanyEvent.EDIT_COMPANY:
                this.editCompanyHandler(companyEvent);
                break;
            case CompanyEvent.CANCEL_COMPANY_AUDIT:
                this.cancelCompanyAuditHandler(companyEvent);
                break;
            default:
                break;
        }
    }

    /**
     * 用户加入公司事件
     */
    public void userJoinCompanyHandler(CompanyEvent companyEvent) {
        Company company = companyEvent.getCompany();
        CompanyEmployee companyEmployee = baseCompanyEmployeeService.getCompanyAdminByCompanyId(company.getCompanyId());
        //用户端管理员通知 + 1
        baseUserTodoStatisticsService.updateCountByUserTodoType(
                EnumUserTodoType.UserJoinCompanyNotifyCount,
                1,
                company.getCompanyId(),
                companyEmployee.getUserId(),
                company.getCompanyId()
        );

    }

    /**
     * 注册公司事件
     */
    public void registerCompanyHandler(CompanyEvent companyEvent) {
        Company company = companyEvent.getCompany();

        //平台端 [企业待审核] + 1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.CompanyWaitAuditCount,
                1,
                company.getCompanyId()
        );

        //刷新redis里面的公司缓存
        baseCompanyService.updateRedisAllCompany();
    }

    /**
     * 修改分配规则事件
     */
    public void editDistributionRuleHandler(CompanyEvent companyEvent) {
        Company company = companyEvent.getCompany();

        //平台端 [企业待审核] + 1
//        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
//                EnumPlatformTodoType.DivideIntoWaitAuditCount,
//                1,
//                company.getCompanyId()
//        );

    }

    /**
     * 修改公司信息事件
     */
    public void editCompanyHandler(CompanyEvent companyEvent) {
        Company company = companyEvent.getCompany();

        //平台端 [企业待审核] + 1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.CompanyWaitAuditCount,
                1,
                company.getCompanyId()
        );

    }

    /**
     * 取消公司审核事件
     */
    public void cancelCompanyAuditHandler(CompanyEvent companyEvent) {
        Company company = companyEvent.getCompany();

        //平台端 [企业待审核] + 1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.CompanyWaitAuditCount,
                -1,
                company.getCompanyId()
        );

    }

    /**
     * 平台审核通过事件
     */
    public void platformAuditPassEventHandler(CompanyEvent companyEvent) {

        //平台端 [企业待审核] + 1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.CompanyWaitAuditCount,
                -1,
                companyEvent.getCompany().getCompanyId()
        );

        Company company = companyEvent.getCompany();
        CompanyEmployee companyEmployee = baseCompanyEmployeeService.getCompanyAdminByCompanyId(company.getCompanyId());
        //用户端 [我的企业] + 1
        baseUserTodoStatisticsService.updateCountByUserTodoType(
                EnumUserTodoType.CompanyAuditNotifyCount,
                1,
                company.getCompanyId(),
                companyEmployee.getUserId(),
                company.getCompanyId()
        );

        //刷新redis里面的公司缓存
        baseCompanyService.updateRedisAllCompany();
    }

    /**
     * 平台审核通过事件
     */
    public void platformAuditNotPassEventHandler(CompanyEvent companyEvent) {

        //平台端 [企业待审核] + 1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.CompanyWaitAuditCount,
                -1,
                companyEvent.getCompany().getCompanyId()
        );

        Company company = companyEvent.getCompany();
        CompanyEmployee companyEmployee = baseCompanyEmployeeService.getCompanyAdminByCompanyId(company.getCompanyId());
        //用户端 [我的企业] + 1
        baseUserTodoStatisticsService.updateCountByUserTodoType(
                EnumUserTodoType.CompanyAuditNotifyCount,
                1,
                company.getCompanyId(),
                companyEmployee.getUserId(),
                company.getCompanyId()
        );
    }

}
